package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @title: SmsController
 * @Author TGE
 * @Date: 2021/5/8 19:25
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SmsController {
    @Autowired
    private SmsService smsService;
    /**
     * 发送短信验证码接口
     * @param pram
     * @return
     */
    @RequestMapping("/login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String,String> pram){
        String phone = pram.get("phone");
        ErrorResult errorResult = null;
        try {
            errorResult = smsService.sendCheckCode(phone);
            if (null == errorResult){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("发送短信验证码失败~ phone = " + phone, e);
            errorResult = ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败！").build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
